package com.zxhtom.application;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @package com.zxhtom.application
 * @Class Huisu
 * @Description TODO
 * @Author zhangxinhua
 * @Date 19-6-11 下午3:33
 */
public class Huisu {
    public static void main(String[] args) {
        Huisu huisu = new Huisu();
        String text = "abcabcbb";
        System.out.println(huisu.len(text));
    }

    public Integer len(String text) {
        List<String> list = getStrArr(text);

        return getMaxLengthFromList(list, 0, 0, list.get(0), 0);
    }

    public Integer getMaxLengthFromList(List<String> list, Integer maxLength,Integer preMax,String source , Integer startIndex ) {
        for (int i = startIndex + 1; i < list.size(); i++) {
            if (!source.equals(list.get(i))) {
                maxLength++;
            } else {
                maxLength = preMax > maxLength ? preMax : maxLength;
                startIndex++;
                return getMaxLengthFromList(list, 0, maxLength,list.get(startIndex), startIndex);
            }
        }
        return maxLength;
    }
    public List<String> getStrArr(String text) {
        List<String> resultList = new ArrayList<>();
        if (StringUtils.isNotEmpty(text)) {
            for (int i = 0; i < text.length(); i++) {
                String singleText = String.valueOf(text.charAt(i));
                resultList.add(singleText);
            }
        }
        return resultList;
    }
}
